/**
    @file       2007_V1_C.c
    @date       2013-09-29 16:13
    @author     Yura Gorishniy
    @version    1.01

    @par        The program determines if user's given weight
                can be found out by using weights 1, 4, 16 etc.
                Every weight can be used only once!

    @note       v.1.01
                - code style improved
                - now main function prints information about the program
                - the structure of CanWeigh function was changed,
                  now there is not so many "if / else" constructions
**/

#include <stdio.h>
#include <stdlib.h>


//#define DEBUG

#ifdef DEBUG
    #define ASSERT(cond)                                                                        \
    if (!(cond))                                                                                \
    {                                                                                           \
        printf("The state is wrong: %s, file: %s, line: %d\n", #cond, __FILE__, __LINE__);      \
        abort();                                                                                \
    }
#else
    #define ASSERT(cond)
#endif //DEBUG


//#define _EJC

#ifdef  _EJC
    #define OUT if (0)
#else
    #define OUT
#endif // _EJC


/**
    CanWeigh - determines the possibility to weigh
               user's given weight by using weights 1, 4, 16 etc.

    @param      weight - the user's given weight

    @return     1 if the possibility to weigh exists
                0 if the possibility to weugh does not exists

    @note       Every weight (1, 4, 16 etc.) can be used once!
**/

int CanWeigh (int weight);


/**
    main        asks for a weight, which the user wans to weigh;
                prints YES if it's possible to weight;
                prints NO  if it's not possible to weight.

    @return     0 if the program finishes successfully
               -1 if input is incorrect
**/

int main ()
{
    int your_weight = 0;

    OUT printf ("# This program determines if user's given weight\n"
                "  can be found out by using weights 1, 4, 16 etc.\n"
                "  (Every weight can be used only once!)\n"
                "# The developer:   Yura Gorishniy      <strausmg@gmail.com>\n"
                "# Version 1.01\n"
                "# The file: %s\n"
                "# The compilation time: %s, %s\n\n", strrchr (__FILE__, '\\'), __DATE__, __TIME__);

    OUT printf ("# Enter the weight (natural) you want to weigh (type it and press ENTER)\n\n");

    if (scanf ("%d", &your_weight) != 1) {printf ("Incorrect weight, try again"); return -1;}

    if (CanWeigh (your_weight))
    {
        OUT printf ("\n");
        printf ("YES");
        OUT printf ("\n" "# It's possible to weigh");
    }
    else
    {
        OUT printf ("\n");
        printf ("NO");
        OUT printf ("\n" "# It's not possible to weigh");
    }

return 0;
}

int CanWeigh (int weight)
{
    if (weight % 4 == 2)
    {
        return 0;
    }

    else if (weight > 4)
            switch (weight % 4)
            {
                case 3:
                    return (CanWeigh (weight / 4 + 1));
                    break;

                case 0: case 1:
                    return (CanWeigh (weight / 4));
                    break;
            }

         else return 1;
}
